package com.lujianfei.gamelearn.mahjong

import DrawInfo
import MahjongModel
import MahjongType
import OnMahjongClickListener
import android.graphics.Rect

class MahjongFactory private constructor() {

    companion object {
        val instance by lazy { MahjongFactory() }
    }

    private val mahjongList = mutableListOf<MahjongModel>()

    fun init() {
        mahjongList.clear()
        initDrawInfo()
    }

    private fun initDrawInfo() {
        val eachWidth = 110
        val eachHeight = 155
        val paddingStart = 10
        val gap = 22
        var top = 0

        for (i in 0 until 9) {
            val left = paddingStart + i * (eachWidth + gap)
            val right = left + eachWidth
            val bottom = top + eachHeight
            mahjongList.add(
                MahjongModel(
                    type = MahjongType.WAN,
                    number = i + 1,
                    drawInfo = DrawInfo(Rect(left, top, right, bottom))
                )
            )
        }

        top += eachHeight
        for (i in 0 until 9) {
            val left = paddingStart + i * (eachWidth + gap)
            val right = left + eachWidth
            val bottom = top + eachHeight
            mahjongList.add(
                MahjongModel(
                    type = MahjongType.TONG,
                    number = i + 1,
                    drawInfo = DrawInfo(Rect(left, top, right, bottom))
                )
            )
        }

        top += eachHeight
        for (i in 0 until 9) {
            val left = paddingStart + i * (eachWidth + gap)
            val right = left + eachWidth
            val bottom = top + eachHeight
            mahjongList.add(
                MahjongModel(
                    type = MahjongType.TIAO,
                    number = i + 1,
                    drawInfo = DrawInfo(Rect(left, top, right, bottom))
                )
            )
        }

        top += eachHeight
        for (i in 0 until 7) {
            val left = paddingStart + i * (eachWidth + gap)
            val right = left + eachWidth
            val bottom = top + eachHeight

            if (i < 4) {
                mahjongList.add(
                    MahjongModel(
                        type = MahjongType.FENG,
                        number = i + 1,
                        drawInfo = DrawInfo(Rect(left, top, right, bottom))
                    )
                )
            } else {
                mahjongList.add(
                    MahjongModel(
                        type = MahjongType.SANYUAN,
                        number = i - 3,
                        drawInfo = DrawInfo(Rect(left, top, right, bottom))
                    )
                )
            }
        }
    }

    fun clone(type:MahjongType, number:Int, onMahjongClickListener: OnMahjongClickListener): MahjongModel {
        val model = mahjongList.firstOrNull { it.type == type && it.number == number }
        return MahjongModel(
            type = model?.type?:MahjongType.WAN,
            number = model?.number?:1,
            drawInfo = DrawInfo(Rect(model?.drawInfo?.srcRect?:Rect())),
            onClick = onMahjongClickListener
        )
    }
}